﻿#region RDIFramework.NET-generated
//------------------------------------------------------------------------------
//     RDIFramework.NET（.NET快速信息化系统开发、整合框架）是基于.NET的快速信息化系统开发、整合框架，给用户和开发者最佳的.Net框架部署方案。
//     RDIFramework.NET平台包含基础公共类库、强大的权限控制、模块分配、数据字典、自动升级、各商业级控件库、工作流平台、代码生成器、开发辅助
//工具等，应用系统的各个业务功能子系统，在系统体系结构设计的过程中被设计成各个原子功能模块，各个子功能模块按照业务功能组织成单独的程序集文
//件，各子系统开发完成后，由RDIFramework.NET平台进行统一的集成部署。
//
// 官方博客：http://www.cnblogs.com/huyong
//           http://blog.csdn.net/chinahuyong
// QQ:80368704
//------------------------------------------------------------------------------
// <auto-generated>
//     此代码由RDIFramework.NET平台代码生成工具自动生成。
//     运行时版本:4.0.30319.1
//
//     对此文件的更改可能会导致不正确的行为，并且如果
//     重新生成代码，这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------
#endregion

using System;
using System.Data;
using System.Collections.Generic;
using System.Reflection;
using System.ServiceModel;

namespace Centaline.BizLogic
{
	using RDIFramework.ServiceAdapter;
	using RDIFramework.Utilities;

	/// <summary>
	/// MaintatinStatusService
	/// 服务层
	/// 
	/// 修改记录
	/// 
	///	2014-04-02 版本：1.0  建立服务层。
	///		
	/// 版本：1.0
	///
	/// <author>
	///	<name></name>
	///	<date>2014-04-02</date>
	/// </author> 
	/// </summary>
	[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
	public class MaintatinStatusService : System.MarshalByRefObject, IMaintatinStatusService
	{
		private string serviceName = "MaintatinStatus";
		
		/// <summary>
		/// RDIFramework.NET平台数据库连接
		/// </summary>
		private readonly string RDIFrameworkDbConection = SystemInfo.RDIFrameworkDbConection;

		/// <summary>
		/// 业务逻辑数据库连接字符串
		/// </summary>
		private readonly string BusinessDbConnection = SystemInfo.BusinessDbConnection;

		#region public string Add(UserInfo userInfo, MaintatinStatusEntity entity, out string statusCode, out string statusMessage) 新增
		/// <summary>
		/// 新增
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="entity">实体</param>
		/// <param name="statusCode">返回状态码</param>
		/// <param name="statusMessage">返回状态消息</param>
		/// <returns>主鍵</returns>
		public string Add(UserInfo userInfo, MaintatinStatusEntity entity, out string statusCode, out string statusMessage)
		{
			string returnValue = string.Empty;            
			statusCode = string.Empty;

			using (IDbProvider frameworkDbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
			{
				try
				{
					frameworkDbProvider.Open(RDIFrameworkDbConection);
					LogManager.Instance.Add(frameworkDbProvider, userInfo, this.serviceName, "新增实体", MethodBase.GetCurrentMethod());

					using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.BusinessDbType))
					{
						try
						{
							dbProvider.Open(BusinessDbConnection);
							MaintatinStatusManager manager = new MaintatinStatusManager(dbProvider, userInfo);
							returnValue = manager.AddEntity(entity);
							// returnValue = manager.Add(entity, out statusCode);
							statusMessage = manager.GetStateMessage(statusCode);
						}
						catch (Exception ex)
						{
							CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
							throw ex;
						}
						finally
						{
							dbProvider.Close();
						}
					}
				}
				catch (Exception ex)
				{
					CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
					throw ex;
				}
				finally
				{
					frameworkDbProvider.Close();
				}
			}		
			return returnValue;
		}
		#endregion

		#region public DataTable GetDT(UserInfo userInfo) 获取列表
		/// <summary>
		/// 获取列表
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <returns>数据表</returns>
		public DataTable GetDT(UserInfo userInfo)
		{
			DataTable dataTable = new DataTable(MaintatinStatusTable.TableName);
			
			using (IDbProvider frameworkDbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
			{
				try
				{
					frameworkDbProvider.Open(RDIFrameworkDbConection);
					LogManager.Instance.Add(frameworkDbProvider, userInfo, this.serviceName, "取得列表", MethodBase.GetCurrentMethod());

					using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.BusinessDbType))
					{
						try
						{
							dbProvider.Open(BusinessDbConnection);
							// 取得列表
							MaintatinStatusManager manager = new MaintatinStatusManager(dbProvider, userInfo);
							dataTable = manager.GetDT(new KeyValuePair<string, object>(MaintatinStatusTable.FieldDeleteMark, 0), MaintatinStatusTable.FieldSortCode);
							dataTable.TableName = MaintatinStatusTable.TableName;
						}
						catch (Exception ex)
						{
							CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
							throw ex;
						}
						finally
						{
							dbProvider.Close();
						}
					}
				}
				catch (Exception ex)
				{
					CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
					throw ex;
				}
				finally
				{
					frameworkDbProvider.Close();
				}
			}

			return dataTable;
		}
		#endregion

		#region public DataTable GetDTByPage(UserInfo userInfo, string searchValue, out int recordCount, int pageIndex = 1, int pageSize = 20, string sortExpression = null) 分页获取列表
		/// <summary>
		/// 分页获取列表
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="searchValue">查询关键字</param>
		/// <param name="recordCount">记录数</param>
		/// <param name="pageIndex">当前页</param>
		/// <param name="pageSize">每页显示记录条数</param>
		/// <param name="sortExpression">排序字段</param>
		/// <returns>数据表</returns>
		public DataTable GetDTByPage(UserInfo userInfo, string searchValue, out int recordCount, int pageIndex = 1, int pageSize = 20, string sortExpression = null)
		{
			DataTable dataTable = new DataTable(MaintatinStatusTable.TableName);
			
			using (IDbProvider frameworkDbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
			{
				try
				{
					frameworkDbProvider.Open(RDIFrameworkDbConection);
					LogManager.Instance.Add(frameworkDbProvider, userInfo, this.serviceName, "取得列表", MethodBase.GetCurrentMethod());

					using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.BusinessDbType))
					{
						try
						{
							dbProvider.Open(BusinessDbConnection);
							// 取得列表
							MaintatinStatusManager manager = new MaintatinStatusManager(dbProvider, userInfo);
							dataTable = manager.GetDTByPage(out recordCount, pageIndex, pageSize, searchValue, sortExpression);
							dataTable.TableName = MaintatinStatusTable.TableName;
						}
						catch (Exception ex)
						{
							CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
							throw ex;
						}
						finally
						{
							dbProvider.Close();
						}
					}
				}
				catch (Exception ex)
				{
					CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
					throw ex;
				}
				finally
				{
					frameworkDbProvider.Close();
				}
			}

			return dataTable;
		}
		#endregion

		#region public MaintatinStatusEntity GetEntity(UserInfo userInfo, string id) 获取实体
		/// <summary>
		/// 得到实体
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="id">主鍵</param>
		/// <returns>实体</returns>
		public MaintatinStatusEntity GetEntity(UserInfo userInfo, string id)
		{
			MaintatinStatusEntity entity = null;

			using (IDbProvider frameworkDbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
			{
				try
				{
					frameworkDbProvider.Open(RDIFrameworkDbConection);
					LogManager.Instance.Add(frameworkDbProvider, userInfo, this.serviceName, "取得实体", MethodBase.GetCurrentMethod());
					
					using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.BusinessDbType))
					{
						try
						{
							dbProvider.Open(BusinessDbConnection);
							MaintatinStatusManager manager = new MaintatinStatusManager(dbProvider, userInfo);
							entity = manager.GetEntity(id);
						}
						catch (Exception ex)
						{
							CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
							throw ex;
						}
						finally
						{
							dbProvider.Close();
						}
					}
				}
				catch (Exception ex)
				{
					CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
					throw ex;
				}
				finally
				{
					frameworkDbProvider.Close();
				}
			}

			return entity;
		}
		#endregion

		#region public int Update(UserInfo userInfo, MaintatinStatusEntity entity, out string statusCode, out string statusMessage) 更新
		/// <summary>
		/// 更新
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="entity">实体</param>
		/// <param name="statusCode">返回状态码</param>
		/// <param name="statusMessage">返回状态信息</param>
		/// <returns>影响行数</returns>
		public int Update(UserInfo userInfo, MaintatinStatusEntity entity, out string statusCode, out string statusMessage)
		{
			int returnValue = 0;
			statusCode = string.Empty;

			using (IDbProvider frameworkDbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
			{
				try
				{
					frameworkDbProvider.Open(RDIFrameworkDbConection);
					LogManager.Instance.Add(frameworkDbProvider, userInfo, this.serviceName, "更新实体", MethodBase.GetCurrentMethod());
					
					using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.BusinessDbType))
					{
						try
						{
							dbProvider.Open(BusinessDbConnection);
							MaintatinStatusManager manager = new MaintatinStatusManager(dbProvider, userInfo);
							returnValue = manager.UpdateEntity(entity);
							// returnValue = manager.Update(entity, out statusCode);
							statusMessage = manager.GetStateMessage(statusCode);
						}
						catch (Exception ex)
						{
							CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
							throw ex;
						}
						finally
						{
							dbProvider.Close();
						}
					}
				}
				catch (Exception ex)
				{
					CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
					throw ex;
				}
				finally
				{
					frameworkDbProvider.Close();
				}
			}

			return returnValue;
		}
		#endregion

		#region public DataTable GetDTByIds(UserInfo userInfo, string[] ids) 根据主键数组获取数据
		/// <summary>
		/// 根据主键数组获取数据
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="ids">主鍵</param>
		/// <returns>数据表</returns>
		public DataTable GetDTByIds(UserInfo userInfo, string[] ids)
		{
			DataTable dataTable = new DataTable(MaintatinStatusTable.TableName);

			using (IDbProvider frameworkDbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
			{
				try
				{
					frameworkDbProvider.Open(RDIFrameworkDbConection);
					LogManager.Instance.Add(frameworkDbProvider, userInfo, this.serviceName, "根据主键数组获取数据", MethodBase.GetCurrentMethod());
					
					using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.BusinessDbType))
					{
						try
						{
							dbProvider.Open(BusinessDbConnection);
							MaintatinStatusManager manager = new MaintatinStatusManager(dbProvider, userInfo);
							dataTable = manager.GetDT(MaintatinStatusTable.FieldId, ids, MaintatinStatusTable.FieldSortCode);
							dataTable.TableName = MaintatinStatusTable.TableName;
						}
						catch (Exception ex)
						{
							CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
							throw ex;
						}
						finally
						{
							dbProvider.Close();
						}
					}
				}
				catch (Exception ex)
				{
					CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
					throw ex;
				}
				finally
				{
					frameworkDbProvider.Close();
				}
			}

			return dataTable;
		}
		#endregion
		
		#region public DataTable GetDTByValues(UserInfo userInfo, List<KeyValuePair<string, object>> parameters) 根据条件获取数据
		/// <summary>
		/// 根据条件获取数据
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="names">字段</param>
		/// <param name="values">值</param>
		/// <returns>数据表</returns>
		public DataTable GetDTByValues(UserInfo userInfo, List<KeyValuePair<string, object>> parameters)
		{
			DataTable dataTable = new DataTable(MaintatinStatusTable.TableName);

			using (IDbProvider frameworkDbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
			{
				try
				{
					frameworkDbProvider.Open(RDIFrameworkDbConection);
					LogManager.Instance.Add(frameworkDbProvider, userInfo, this.serviceName, "根据条件获取数据", MethodBase.GetCurrentMethod());
					
					using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.BusinessDbType))
					{
						try
						{
							dbProvider.Open(BusinessDbConnection);
							// 根据条件获取数据
							MaintatinStatusManager manager = new MaintatinStatusManager(dbProvider, userInfo);
							dataTable = manager.GetDT(parameters);
							dataTable.TableName = MaintatinStatusTable.TableName;
						}
						catch (Exception ex)
						{
							CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
							throw ex;
						}
						finally
						{
							dbProvider.Close();
						}
					}
				}
				catch (Exception ex)
				{
					CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
					throw ex;
				}
				finally
				{
					frameworkDbProvider.Close();
				}
			}
			return dataTable;
		}
		#endregion
		
		#region public int BatchSave(UserInfo userInfo, List<MaintatinStatusEntity> entites) 批量保存数据
		/// <summary>
		/// 批量保存数据
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="entites">实体列表</param>
		/// <returns>影响行数</returns>
		public int BatchSave(UserInfo userInfo, List<MaintatinStatusEntity> entites)
		{
			int returnValue = 0;

			using (IDbProvider frameworkDbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
			{
				try
				{
					frameworkDbProvider.Open(RDIFrameworkDbConection);
					LogManager.Instance.Add(frameworkDbProvider, userInfo, this.serviceName, "批量保存数据", MethodBase.GetCurrentMethod());

					using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.BusinessDbType))
					{
						try
						{
							dbProvider.Open(BusinessDbConnection);
							MaintatinStatusManager manager = new MaintatinStatusManager(dbProvider, userInfo);
							// returnValue = manager.BatchSave(entites);
							foreach (var entity in entites)
							{
								returnValue += manager.Update(entity);
							}
						}
						catch (Exception ex)
						{
							CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
							throw ex;
						}
						finally
						{
							dbProvider.Close();
						}
					}
				}
				catch (Exception ex)
				{
					CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
					throw ex;
				}
				finally
				{
					frameworkDbProvider.Close();
				}
			}

			return returnValue;
		}
		#endregion
		
		#region public int Delete(UserInfo userInfo, string id) 删除指定数据
		/// <summary>
		/// 删除指定数据
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="id">主鍵</param>
		/// <returns>数据表</returns>
		public int Delete(UserInfo userInfo, string id)
		{
			int returnValue = 0;

			using (IDbProvider frameworkDbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
			{
				try
				{
					frameworkDbProvider.Open(RDIFrameworkDbConection);
					LogManager.Instance.Add(frameworkDbProvider, userInfo, this.serviceName, "删除指定数据", MethodBase.GetCurrentMethod());
					
					using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.BusinessDbType))
					{
						try
						{
							dbProvider.Open(BusinessDbConnection);
							MaintatinStatusManager manager = new MaintatinStatusManager(dbProvider, userInfo);
							returnValue = manager.Delete(id);
						}
						catch (Exception ex)
						{
							CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
							throw ex;
						}
						finally
						{
							dbProvider.Close();
						}
					}
				}
				catch (Exception ex)
				{
					CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
					throw ex;
				}
				finally
				{
					frameworkDbProvider.Close();
				}
			}

			return returnValue;
		}
		#endregion

		#region public int BatchDelete(UserInfo userInfo, string[] ids) 批量删除数据
		/// <summary>
		/// 批量删除数据
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="ids">主键数组</param>
		/// <returns>影响行数</returns>
		public int BatchDelete(UserInfo userInfo, string[] ids)
		{
			int returnValue = 0;

			using (IDbProvider frameworkDbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
			{
				try
				{
					frameworkDbProvider.Open(RDIFrameworkDbConection);
					LogManager.Instance.Add(frameworkDbProvider, userInfo, this.serviceName, "批量删除数据", MethodBase.GetCurrentMethod());

					using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.BusinessDbType))
					{
						try
						{
							dbProvider.Open(BusinessDbConnection);
							// 开始事务
							dbProvider.BeginTransaction();
							MaintatinStatusManager manager = new MaintatinStatusManager(dbProvider, userInfo);
							returnValue = manager.Delete(ids);
							// 提交事务
							dbProvider.CommitTransaction();
						}
						catch (Exception ex)
						{
							// 回滚事务
							dbProvider.RollbackTransaction();
							CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
							throw ex;
						}
						finally
						{
							dbProvider.Close();
						}
					}
				}
				catch (Exception ex)
				{
					CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
					throw ex;
				}
				finally
				{
					frameworkDbProvider.Close();
				}
			}

			return returnValue;
		}
		#endregion

		#region public int SetDeleted(UserInfo userInfo, string[] ids) 批量设置删除标志
		/// <summary>
		/// 批量设置删除标志
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="ids">主键数组</param>
		/// <returns>影响行数</returns>
		public int SetDeleted(UserInfo userInfo, string[] ids)
		{
			int returnValue = 0;

			using (IDbProvider frameworkDbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
			{
				try
				{
					frameworkDbProvider.Open(RDIFrameworkDbConection);
					LogManager.Instance.Add(frameworkDbProvider, userInfo, this.serviceName, "批量设置删除标志", MethodBase.GetCurrentMethod());
					
					using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.BusinessDbType))
					{
						try
						{
							dbProvider.Open(BusinessDbConnection);
							MaintatinStatusManager manager = new MaintatinStatusManager(dbProvider, userInfo);
							returnValue = manager.SetDeleted(ids);
						}
						catch (Exception ex)
						{
							CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
							throw ex;
						}
						finally
						{
							dbProvider.Close();
						}
					}
				}
				catch (Exception ex)
				{
					CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
					throw ex;
				}
				finally
				{
					frameworkDbProvider.Close();
				}
			}

			return returnValue;
		}
		#endregion
	}
}
